package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.apache.ibatis.annotations.MapKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public void insertBed(BedDto bedDto) {
        LocalDateTime now = LocalDateTime.now();
        Bed bed = BeanUtil.toBean(bedDto, Bed.class);
        bed.setBedStatus(0);
        bed.setCreateTime(now);
        bed.setUpdateTime(now);
        bed.setCreateBy(1L);
        bedMapper.insertBed(bed);
    }

    @Override
    @MapKey("id")
    public BedVo getBedsById(Long id) {
        Bed bed = bedMapper.getBedsById(id);
        BedVo vo = new BedVo();
        BeanUtil.copyProperties(bed, vo);
        return vo;
    }

    @Override
    public int updateBed(BedDto bedDto) {
        if (bedDto != null) {
            Bed bed = BeanUtil.toBean(bedDto, Bed.class);
            bed.setUpdateTime(LocalDateTime.now());
            bed.setUpdateBy(1L);
            bedMapper.updateBed(bed);
            return 1;
        }
        return 0;
    }

    @Override
    public int deleteBed(Integer id) {
        if (id != null) {
            bedMapper.deleteBed(id);
            return 1;
        }
        return 0;
    }


}

